var countdown=require('../public/javascripts/countdown'),sdc = require('sdc-clients')

var dc_config=require('../config/dc')['poc']
	,vmapi = new sdc.VMAPI(dc_config.vmapi)
module.exports = {
	view:function(req, res) {
		if(req.headers.userid&&req.headers.userid!='null'){
			var limit=req.params.limit||10000,page=req.query.page||1,start=new Date()
			return vmapi.listVms({query:"(&(|(state=stopped)(state=running))(owner_uuid="+req.headers.userid+"))",limit:limit,offset:(page-1) * limit},function(err, vms, _req, _res){
				var end=new Date()
				if (err) {
					return console.log(err)
				}
				console.log(countdown(start,end).toString())
				require('./packages').list(req, res, function(){
					require('./images').listImages(req, res, function(){
						res.send({vms:vms,packages:req.packages,images:req.images,path:'vms.html'});
					})
				})
			})
		}
		return res.send({path:'loginView.html'})
	}
}